//
//  PictureListSelectLabelView.m
//  jingdongfang
//
//  Created by RKL on 2018/5/14.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "PictureListSelectLabelView.h"
#import "FilterReusableView.h"
#import "FilterCell.h"
#import "IGArtLibraryApi.h"

@interface PictureListSelectLabelView ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate>
{
    NSArray *titleArr;
    //    NSMutableArray *staueArr;//0,1,2
    //    BOOL isShowMore;
    //    BOOL isShowMore2;
    UILabel *titleLabel;
}
@property (nonatomic, strong)UIView *hiddenView;
@property (nonatomic, strong)UIView *backView;
@property (nonatomic, strong)UIButton *resetBtn;
@property (nonatomic, strong)UIButton *sureBtn;
@end


@implementation PictureListSelectLabelView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self loadUI:frame];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)loadUI:(CGRect)frame
{
    self.hiddenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width / 5, self.height)];
    self.hiddenView.alpha = 0.7;
    self.hiddenView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.hiddenView];
    
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(self.width / 5, 0, self.width / 5 * 4, self.height)];
    self.backView.alpha = 1.0;
    self.backView.backgroundColor = [UIColor c3Color];
    [self addSubview:self.backView];
    
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.backView.width - 100) / 2, 30, 100, 20)];
    titleLabel.backgroundColor = [UIColor c3Color];
    titleLabel.textAlignment= NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor c4Color];
    titleLabel.font = [UIFont systemFontOfSize:18];
    titleLabel.text = @"标签选择";
    [self.backView addSubview:titleLabel];
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, self.backView.width, self.backView.height - 50 - 50) collectionViewLayout:layout];
    self.collectionV.backgroundColor = [UIColor c3Color];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.collectionV registerNib:[UINib nibWithNibName:@"FilterCell" bundle:nil] forCellWithReuseIdentifier:@"FilterCell"];
    
    [self.collectionV registerNib:[UINib nibWithNibName:@"FilterReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FilterReusableView"];
    [self.backView addSubview:self.collectionV];
    
    self.resetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [self.resetBtn setTitle:@"重置" forState:UIControlStateNormal];
    [self.resetBtn setTitle:NSLocalizedString(@"Reset", nil) forState:UIControlStateNormal];
    [self.resetBtn setTitleColor:[UIColor c4Color] forState:UIControlStateNormal];
    self.resetBtn.backgroundColor = [UIColor c3Color];
    [self.resetBtn addTarget:self action:@selector(resetBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.backView addSubview:self.resetBtn];
    
    self.sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [self.sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.sureBtn setTitle:NSLocalizedString(@"Confirm", nil) forState:UIControlStateNormal];
    [self.sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.sureBtn.backgroundColor = [UIColor appColor];
    [self.sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.backView addSubview:self.sureBtn];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.backView.frame = CGRectMake(self.width / 5, 0, self.width / 5 * 4, self.height);
    
    titleLabel.frame = CGRectMake((self.backView.width - 100) / 2, 30, 100, 20);
    
    self.collectionV.frame = CGRectMake(0, 50, self.backView.width, self.backView.height - 50 - 50);
    [self.collectionV reloadData];
    
    self.resetBtn.size = CGSizeMake(self.backView.width / 2, 50);
    self.resetBtn.x = 0;
    self.resetBtn.bottom = self.backView.bottom;
    
    self.sureBtn.size = CGSizeMake(self.backView.width / 2, 50);
    self.sureBtn.x = self.resetBtn.width;
    self.sureBtn.bottom = self.backView.bottom;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataSource.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    IGArtLibrary *lib = self.dataSource[section];
    return self.dataSource.count ? [lib.lstSubClass count] : 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataSource && self.dataSource.count > 0)
    {
        FilterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FilterCell" forIndexPath:indexPath];
        IGArtLibrary *lib = self.dataSource[indexPath.section];
        IGArtDetailLibrary *detailLib = lib.lstSubClass[indexPath.item];
        cell.titleL.text = detailLib.title;
        cell.sel = [detailLib.isSelect isEqualToString:@"1"] ? YES : NO;
        [cell setNeedsLayout];
        return cell;
    }
    return nil;
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    NSArray *arr = self.dataSource[indexPath.section];
    //    NSString *str = arr[indexPath.item][@"title"];
    //    CGFloat width = [Tool widthForLabel:[NSString stringWithFormat:@"%@",str] fontSize:13];
    //    return CGSizeMake(width + 30, 30);
    return CGSizeMake((self.backView.width - 61) / 3, 30);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    FilterReusableView *reusableV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FilterReusableView" forIndexPath:indexPath];
    if (!reusableV)
    {
        reusableV = [[NSBundle mainBundle] loadNibNamed:@"FilterReusableView" owner:self options:nil].firstObject;
    }
    IGArtLibrary *lib = self.dataSource[indexPath.section];
    reusableV.typeL.text = lib.title;
    reusableV.moreBtn.hidden = YES;
    
    return reusableV;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    IGArtLibrary *lib = self.dataSource[section];
    if (lib.lstSubClass.count > 0)
    {
        return CGSizeMake(self.backView.width, 60);
    }
    return CGSizeZero;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionVeliew layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 15, 10, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.filterBlock(self.dataSource);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    IGArtLibrary *lib = self.dataSource[indexPath.section];
    IGArtDetailLibrary *detailLib = lib.lstSubClass[indexPath.item];
    detailLib.isSelect = [detailLib.isSelect isEqualToString:@"1"] ? @"0" : @"1";
    
    if (indexPath.section == 0)
    { //第一行分类只能选一个
        NSInteger selsectedNum = 0;
        for (IGArtDetailLibrary *detailbil in lib.lstSubClass)
        {
            if ([detailbil.isSelect isEqualToString:@"1"])
            {
                selsectedNum ++;
            }
        }
        if (selsectedNum > 1)
        {
            NSLog(@"超过1个");
            detailLib.isSelect = [detailLib.isSelect isEqualToString:@"1"] ? @"0" : @"1";
        }
    }
    
    NSInteger selsectedNum = 0;
    for (IGArtLibrary *lib in self.dataSource)
    {
        for (IGArtDetailLibrary *detailbil in lib.lstSubClass)
        {
            if ([detailbil.isSelect isEqualToString:@"1"])
            {
                selsectedNum ++;
            }
        }
    }
    if (selsectedNum > 3)
    {
        NSLog(@"超过3个");
        detailLib.isSelect = [detailLib.isSelect isEqualToString:@"1"] ? @"0" : @"1";
    }
    
    [self.collectionV reloadItemsAtIndexPaths:@[indexPath]];
}

- (void)resetBtnClick
{
    self.dataSource = [self addSelectStrWithArr:self.dataSource];
    [self.collectionV reloadData];
}

- (void)sureBtnClick
{
    NSMutableArray * tempArr = [NSMutableArray arrayWithCapacity:10];
    for (IGArtLibrary *lib in self.dataSource)
    {
        for (IGArtDetailLibrary *detailbil in lib.lstSubClass)
        {
            if ([detailbil.isSelect isEqualToString:@"1"])
            {
                [tempArr addObject:detailbil];
            }
        }
    }
    
    self.sureBlock(tempArr);
}

- (NSMutableArray *)addSelectStrWithArr:(NSMutableArray *)arr
{
    NSMutableArray *temp = [arr mutableCopy];
    for (int i = 0; i < arr.count; i++)
    {
        IGArtLibrary *lib = arr[i];
        IGArtLibrary *libTemp = temp[i];
        for (int j = 0; j < lib.lstSubClass.count; j++)
        {
            IGArtDetailLibrary *dic = libTemp.lstSubClass[j];
            dic.isSelect = @"0";
        }
    }
    return temp;
}
@end

















